# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os

import requests

picture_dir = "E:/Picture"
url_pic = "https://fitment.chenyoujiu.com"


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
    items = name.split('/')
    print(name.split('/'))
    r_name = name.replace('/'+items[len(items)-1], '')
    print(r_name)


def header():
    headers = {
        'Host': 'fitment.chenyoujiu.com',
        'Pragma': 'no-cache',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat',
    }
    return headers


def save_picture_info(item):
    p_items = item.split('/')
    pic_dir_name = picture_dir + item
    pic_dir = picture_dir+item.replace('/'+p_items[len(p_items)-1], '')
    if not os.path.exists(pic_dir):  # 如果不存在路径，则创建这个路径，关键函数就在这两行，其他可以改变
        os.makedirs(pic_dir)
    try:
        response = requests.get(url_pic + item, headers=header())
        if response.status_code == 200:
            page_data = response.content
            with open(pic_dir_name, 'wb') as f:
                f.write(page_data)
            return None
    except requests.RequestException:
        return None


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # print_hi('/uploads/images/2020/12/18/0e14186ac29d21861ae0012cc9e211c4.jpg')
    # save_picture_info('/uploads/pic/2020/12/18/517942a06e13e9a84c613f2d879aedc0_thumb.png')
    save_picture_info('/uploads/pic/2020/09/14/3b0f88b06ed00fcdd534c9c75d311135.jpg')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
